package zk.dwj.cn;

import org.apache.zookeeper.*;

/**
 * Created by Administrator on 2017/11/13 0013.
 */
public class CreateNodeSync implements Watcher{

    private static ZooKeeper zooKeeper;

    public void process(WatchedEvent watchedEvent) {
        System.out.print("收到事件：" + watchedEvent);
        if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
            doSomething();
        }
    }

    private void doSomething() {//同步创建
        System.out.println("do some thing");
        try {
            String path = zooKeeper.create("/node_1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("return path : " + path);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            zooKeeper = new ZooKeeper("192.168.110.134:2181", 5000, new CreateNodeSync());
            System.out.println(zooKeeper.getState());
            Thread.sleep(Integer.MAX_VALUE);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
